﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using EngineOfEvermore.Editor.Common;
using EngineOfEvermore.Common;

namespace EngineOfEvermore.Editor.SceneEditor.View
{
    partial class Document : EngineOfEvermore.Editor.Common.SceneView, IDocumentView
    {
        TabPage _owningTabPage;
        readonly SceneModel _model;
        DocumentController _controller;
        bool _isActiveDocument;

        public Document( SceneModel model, TabPage owner )
        {
            Debug.Assert( model != null );
            this.InitializeComponent();

            _model = model;
            _owningTabPage = owner;

            _updateTabText();

            _model.NameOfAssociatedResourceChanged += _modelNameOfAssociatedResourceChanged;
            _model.HasUnsavedChangesChanged += _modelHasUnsavedChangesChanged;

            this.SceneGraph = model.SceneGraph;
        }

        public bool IsActiveDocument
        {
            get { return _isActiveDocument; }
            set 
            {
                if ( value != _isActiveDocument )
                {
                    _isActiveDocument = value;
                    this.DoRender = _isActiveDocument;
                }
            }
        }

        void _modelHasUnsavedChangesChanged( SceneModel obj )
        {
            _updateTabText();
        }

        void _modelNameOfAssociatedResourceChanged( SceneModel obj )
        {
            _updateTabText();
        }

        private void _updateTabText()
        {
            if ( String.IsNullOrEmpty( _model.NameOfAssociatedResource ) )
            {
                _owningTabPage.Text = "*Unsaved SceneGraph";
            }
            else
            {
                if ( _model.HasUnsavedChanges )
                {
                    _owningTabPage.Text = "*" + _model.NameOfAssociatedResource;
                }
                else
                {
                    _owningTabPage.Text = _model.NameOfAssociatedResource;
                }
            }
        }

        public void InitializeController( DocumentController controller )
        {
            Debug.Assert( controller != null );
            _controller = controller;
        }

        public SceneModel Model
        {
            get { return _model; }
        }

        void Document_MouseDown( object sender, MouseEventArgs e )
        {
            _controller.HandleInputEvent( e, InputType.KeyDown, _model );
        }

        void Document_MouseUp( object sender, MouseEventArgs e )
        {
            _controller.HandleInputEvent( e, InputType.KeyUp, _model );
        }

        void Document_KeyDown( object sender, KeyEventArgs e )
        {
            _controller.HandleInputEvent( e, InputType.KeyDown, _model );
        }

        private void Document_KeyUp( object sender, KeyEventArgs e )
        {
            _controller.HandleInputEvent( e, InputType.KeyUp, _model );
        }

        private void Document_MouseMove( object sender, MouseEventArgs e )
        {
            _controller.HandleInputEvent( e, InputType.MouseMove, _model );
        }

        private void Document_DragEnter( object sender, DragEventArgs e )
        {
            DragDropEffects realEffect = DragDropEffects.None;

            if ( e.AllowedEffect == DragDropEffects.Move )
            {
                if ( e.Data.GetDataPresent( typeof( TreeNode ) ) )
                {
                    TreeNode node = (TreeNode) e.Data.GetData( typeof( TreeNode ) );

                    if ( node.Tag != null && node.Tag is ResourceExplorer.NodeData )
                    {
                        if ( _controller.AllowResourceExplorerNodeDragEnter( node ) )
                        {
                            realEffect = DragDropEffects.Move;
                        }
                    }
                }
            }

            e.Effect = realEffect;
        }

        private void Document_DragDrop( object sender, DragEventArgs e )
        {
            Debug.Assert( e.Data.GetDataPresent( typeof( TreeNode ) ) );

            TreeNode node = (TreeNode) e.Data.GetData( typeof( TreeNode ) );
            Debug.Assert( node.Tag != null && node.Tag is ResourceExplorer.NodeData );

            var clientCoords = (Vector2d) this.PointToClient( new Point( e.X, e.Y ) );

            _controller.TreeNodeDropped( node, clientCoords );
        }
    }

    public interface IDocumentView
    {
        void InitializeController( DocumentController controller );
        SceneModel Model { get; }
        EngineOfEvermore.Imaging.Camera Camera { get; }
    }
}
